package o;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import o.fuc.c;
import o.fus.a;

class bno {
    public static int ˊ = 2000;
    private static int ˋ = 10000;
    private static fuv ˎ;
    private static volatile bno ˏ = null;
    private static int ॱ = 10000;

    public static class b {
        private Exception ˋ = null;
        private boolean ˎ = false;
        private ftz ˏ = null;
        private fvb ॱ = null;

        public boolean ˊ() {
            return this.ˎ;
        }

        public void ˋ(boolean z) {
            this.ˎ = z;
        }

        public Exception ˎ() {
            return this.ˋ;
        }

        public void ॱ(Exception exception) {
            this.ˋ = exception;
        }

        public fvb ˏ() {
            return this.ॱ;
        }

        public void ˎ(fvb o_fvb) {
            this.ॱ = o_fvb;
        }

        public ftz ॱ() {
            return this.ˏ;
        }

        public void ˊ(ftz o_ftz) {
            this.ˏ = o_ftz;
        }
    }

    public static class d {
        protected long ˊ = -1;
        protected String ˋ;
        protected boolean ˎ;
        protected List<String> ˏ = null;
        protected long ॱ = -1;
        protected String ॱॱ;
    }

    static class e implements Callable {
        private d ˋ;

        public /* synthetic */ Object call() throws Exception {
            return ˋ();
        }

        public e(d dVar) {
            this.ˋ = dVar;
        }

        public b ˋ() {
            b bVar = new b();
            try {
                a ॱ = new a().ˎ(bno.ˊ(this.ˋ.ˋ)).ˏ(new c().ˎ().ˊ()).ॱ("Accept-Encoding", "identity").ॱ("Cache-Control", "no-cache");
                String str = "";
                if (this.ˋ.ॱ >= 0 && this.ˋ.ˊ >= this.ˋ.ॱ) {
                    str = "bytes=" + this.ˋ.ॱ + "-" + this.ˋ.ˊ;
                }
                if (!dof.ˋ(str)) {
                    ॱ.ॱ("Range", str);
                }
                if (!dof.ॱ(this.ˋ.ॱॱ) && bni.ॱ(new URL(this.ˋ.ˋ).getHost())) {
                    ॱ.ॱ("Host", this.ˋ.ॱॱ);
                }
                fuv ॱ2 = bno.ˎ;
                if (!this.ˋ.ˎ) {
                    ॱ2 = bno.ˎ.ˉ().ˎ(false).ॱ(false).ˏ(true).ˋ();
                }
                if (this.ˋ.ˏ != null) {
                    ॱ.ˊ(new ArrayList(this.ˋ.ˏ));
                }
                ॱ.ˋ(true);
                ftz ˎ = ॱ2.ˎ(ॱ.ॱ());
                bVar.ˊ(ˎ);
                fvb execute = ˎ.execute();
                bVar.ˋ(true);
                bVar.ˎ(execute);
            } catch (Exception e) {
                bVar.ॱ(e);
            }
            return bVar;
        }
    }

    bno() {
    }

    public static void ॱ(int i) {
        if (i > 0) {
            bnc.ˎ.ˋ("HiAppDownload", "set connect timeout:" + i);
            ˋ = i;
        }
    }

    public static void ˊ(int i) {
        if (i > 0) {
            bnc.ˎ.ˋ("HiAppDownload", "set read write timeout:" + i);
            ॱ = i;
        }
    }

    public static long ˎ(String str) {
        long j = -1;
        if (dof.ˋ(str) || !str.startsWith("bytes")) {
            return -1;
        }
        int indexOf = str.indexOf(47);
        if (-1 != indexOf) {
            try {
                j = Long.parseLong(str.substring(indexOf + 1));
                bnc.ˎ.ˎ("HiAppDownload", "get new fileLength by Content-Range:" + j);
                return j;
            } catch (NumberFormatException e) {
                bnc.ˎ.ˊ("HiAppDownload", "getEntityLength NumberFormatException");
                return j;
            }
        }
        bnc.ˎ.ˊ("HiAppDownload", "getEntityLength failed Content-Range");
        return -1;
    }

    public static b ˎ(bnw o_bnw, d dVar) {
        Exception e;
        b bVar = null;
        if (!(ˎ == null || dof.ˋ(dVar.ˋ))) {
            ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
            b bVar2 = new b();
            Future submit = newSingleThreadExecutor.submit(new e(dVar));
            if (o_bnw != null) {
                o_bnw.ˊ(submit);
            }
            try {
                bVar = (b) submit.get((long) (ˋ + 1000), TimeUnit.MILLISECONDS);
                try {
                    if (bny.ˎ(1121)) {
                        int ˏ = bns.ॱ().ˏ();
                        if (ˏ <= 0 || (o_bnw != null && ˏ == o_bnw.ˊ())) {
                            bVar.ˋ(false);
                            throw new SocketTimeoutException("mock SocketTimeoutException");
                        }
                    }
                } catch (TimeoutException e2) {
                    bVar.ॱ(new SocketTimeoutException("connect timeout"));
                    return bVar;
                } catch (Exception e3) {
                    e = e3;
                    bVar.ॱ(e);
                    return bVar;
                }
            } catch (TimeoutException e4) {
                bVar = bVar2;
                bVar.ॱ(new SocketTimeoutException("connect timeout"));
                return bVar;
            } catch (Exception e5) {
                Exception exception = e5;
                bVar = bVar2;
                e = exception;
                bVar.ॱ(e);
                return bVar;
            }
        }
        return bVar;
    }

    public static String ˊ(String str) {
        ConnectivityManager connectivityManager = (ConnectivityManager) bnl.ˏ().ॱ().getSystemService("connectivity");
        if (connectivityManager != null) {
            try {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                fur ॱ = fur.ॱ(str);
                if (ॱ != null) {
                    str = ॱ.ˏॱ().ˎ("net", String.valueOf(doo.ˊ(activeNetworkInfo))).ॱ().toString();
                }
            } catch (Exception e) {
                bnc.ˎ.ˊ("HiAppDownload", "addNetInfoToUrl exception:" + e.getMessage());
            }
        }
        return str;
    }

    public static String ˊ(String str, String str2) {
        try {
            URI uri = new URI(str);
            URI uri2 = new URI(uri.getScheme(), null, str2, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
            bnc.ˎ.ˋ("HiAppDownload", "replaceUrlHost:url updated");
            str = uri2.toString();
        } catch (URISyntaxException e) {
        }
        return str;
    }

    public static String ˏ(String str) {
        if (str == null) {
            return "";
        }
        if (str.contains("failed to connect to") && str.contains("from")) {
            return str.replaceFirst("from (.*)after", "after");
        }
        return str;
    }

    public static String ˏ(String str, String str2) {
        if (str == null) {
            return null;
        }
        try {
            return str2 + new URL(str).getFile();
        } catch (MalformedURLException e) {
            bnc.ˎ.ˊ("HiAppDownload", "updataIP exception:" + e.getMessage());
            return str;
        }
    }

    public static boolean ˎ(int i) {
        return i == 112 || i == 113 || (i >= 1130 && i <= 1139);
    }

    public static boolean ˋ(Exception exception) {
        return (exception instanceof SocketTimeoutException) || (exception.getCause() instanceof SocketTimeoutException);
    }

    public static String ˋ(String str) {
        String str2 = null;
        if (str != null) {
            try {
                str2 = new URL(str).getHost();
            } catch (MalformedURLException e) {
                bnc.ˎ.ˊ("HiAppDownload", "getUrlHost:" + e.getMessage());
            }
        }
        return str2;
    }

    public static void ˋ(fuv o_fuv) {
        o.fuv.b ˎ = o_fuv.ˉ().ˊ((long) ˋ, TimeUnit.MILLISECONDS).ˋ((long) ॱ, TimeUnit.MILLISECONDS).ॱ(Arrays.asList(new fut[]{fut.ˋ})).ॱ(true).ˎ(true);
        ˎ.ˎ((long) ˋ, TimeUnit.MILLISECONDS);
        ˎ = ˎ.ˋ();
    }

    public static boolean ॱ(bnj o_bnj) {
        int ˊ = o_bnj.ˊ();
        if (ˊ == 100 || ˊ == 102 || ˊ == 121 || ˊ == DnsConfig.MAX_CACHE_ENTRIES || ˊ == 117 || ˊ == 129 || ˊ == 126) {
            return true;
        }
        return false;
    }

    public static int ˎ(long j) {
        if (j > 838860800) {
            return 3;
        }
        if (j < 2097152) {
            return 1;
        }
        return 2;
    }
}
